package cumeo89.chii.client.ui;

import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.SimplePanel;

import cumeo89.chii.client.images.ImageFactory;

public class TristateCheckBox extends Composite {

	public interface TristateCheckboxImages extends ImageBundle {

		AbstractImagePrototype unchanged();

		AbstractImagePrototype checked();

		AbstractImagePrototype unchecked();

	}

	private HorizontalPanel pnlContainer = new HorizontalPanel();
	private SimplePanel pnlImage = new SimplePanel();
	private Image imgUnchanged, imgChecked, imgUnchecked;
	private HTML htmHTML = new HTML();

	public TristateCheckBox(String html) {
		this(ImageFactory.IMAGES, html);
	}

	public TristateCheckBox(TristateCheckboxImages images, String html) {
		imgChecked = images.checked().createImage();
		imgUnchanged = images.unchanged().createImage();
		imgUnchecked = images.unchecked().createImage();

		pnlContainer.setSpacing(3);
		pnlContainer.add(pnlImage);
		pnlContainer.setCellVerticalAlignment(pnlImage,
				HasVerticalAlignment.ALIGN_MIDDLE);
		pnlContainer.add(htmHTML);
		initWidget(pnlContainer);

		setHTML(html);
		resetState();
	}

	public void setHTML(String html) {
		htmHTML.setHTML(html);
	}

	public boolean isChecked() {
		return imgChecked.isVisible();
	}

	public void setChecked(boolean checked) {
		if (checked) {
			pnlImage.setWidget(imgChecked);
		} else {
			pnlImage.setWidget(imgUnchecked);
		}
	}

	public boolean isChanged() {
		return !imgUnchanged.isVisible();
	}

	public void resetState() {
		pnlImage.setWidget(imgUnchanged);
	}

}
